package com.tool.cn.util;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import com.sun.mail.util.MailSSLSocketFactory;

/**
 * Email 工具类
 * 此例是qq邮箱发送的，需要先设置开启IMAP，获取授权码。
 * 
 * lyrghnzcyhzldbgc
 * @author wangzh 2018年1月11日 上午10:05:10
 */
public class EmailUtil {

	public static void main(String[] args) {
		sendEmail("新的开始", 1);
	}
	
	/**
	 * 发送邮件
	 * @param title 邮件主题
	 * @param type 邮件类型 1 文本  2 html 3 内容嵌套图片  4 附件  5 发送内嵌图片和附件邮件
	 */
	public static void sendEmail(String title,int type){
		try {
			Properties props = new Properties();
			// 发送服务器需要身份验证
			props.setProperty("mail.smtp.auth", "true");
			// 设置邮件服务器主机名
			props.setProperty("mail.host", "smtp.qq.com");
			// 发送邮件协议名称
			props.setProperty("mail.transport.protocol", "smtp");
			//ssl 加密
			MailSSLSocketFactory sf = new MailSSLSocketFactory();
			sf.setTrustAllHosts(true);
			props.put("mail.smtp.ssl.enable", "true");
			props.put("mail.smtp.ssl.socketFactory", sf);
			
			Session session = Session.getInstance(props);

			Message msg = new MimeMessage(session);
			// 邮件主题
			msg.setSubject(title);
			//发送时间	
			msg.setSentDate(new Date());
			if(type==1){
				sendText(msg);
			}else if(type==2){
				sendHtml(msg);
			}else if(type==3){
				sendImg(msg);
			}else if(type==4){
				sendAttachment(msg);
			}else if(type==5){
				sendImageAndAttachment(msg);
			}
			
			//邮件发送者
			msg.setFrom(new InternetAddress("发送方qq邮箱"));
			// 创建邮件的接收者地址，并设置到邮件消息中
			msg.setRecipient(Message.RecipientType.TO, new InternetAddress("接收方qq邮箱"));
			Transport transport = session.getTransport();
			transport.connect("smtp.qq.com", "发送方qq邮箱", "授权码");  //账户、授权码

			transport.sendMessage(msg, new Address[] { new InternetAddress("接收方qq邮箱") });
			transport.close();
			
		} catch (Exception e) {
			System.err.println(e.toString());
		}
	}
	
	/**
	 * 发送文本文件
	 */
	private static void sendText(Message msg) throws MessagingException{	
		String context="相信未来更好";
		msg.setText(context);
			
	}
	
	/**
	 * 发送html
	 */
	private static void sendHtml(Message msg) throws MessagingException{
			//邮件内容
			Multipart mainPart = new MimeMultipart();
			// 创建一个包含HTML内容的MimeBodyPart
			BodyPart html = new MimeBodyPart();
			// 设置HTML内容
			html.setContent(
					"<html><body><img src='http://avatar.csdn.net/A/C/A/1_jianggujin.jpg'/><div>this is a HTML email.</div></body></html>",
					"text/html; charset=utf-8");
			mainPart.addBodyPart(html);
			// 将MiniMultipart对象设置为邮件内容
			msg.setContent(mainPart);

	}
	
	/**
	 * 发送内容嵌套图片
	 */
	private static void sendImg(Message msg) throws MessagingException{
			//邮件内容
			MimeMultipart multipart = new MimeMultipart("related");

			BodyPart messageBodyPart = new MimeBodyPart();
			String htmlText = "<html><body><img src='cid:image'/><div>this is a HTML email.</div></body></html>";
			messageBodyPart.setContent(htmlText, "text/html; charset=utf-8");
			multipart.addBodyPart(messageBodyPart);

			MimeBodyPart imageBodyPart = new MimeBodyPart();
			DataSource fds = new FileDataSource("1_jianggujin.jpg");
			imageBodyPart.setDataHandler(new DataHandler(fds));
			imageBodyPart.setContentID("image");
			multipart.addBodyPart(imageBodyPart);
			
			
			msg.setContent(multipart);
	}
	
	/**
	 * 发送附件
	 */
	private static void sendAttachment(Message msg) throws UnsupportedEncodingException, MessagingException{
			//邮件内容
			MimeMultipart multipart = new MimeMultipart("mixed");
			
			BodyPart messageBodyPart = new MimeBodyPart();
			String htmlText = "<html><body><div>this is a Attachment email.this email has a attachment!</div></body></html>";
			messageBodyPart.setContent(htmlText, "text/html; charset=utf-8");
			multipart.addBodyPart(messageBodyPart);

			MimeBodyPart imageBodyPart = new MimeBodyPart();
			File imageFile = new File("1_jianggujin.jpg");
			DataSource fds = new FileDataSource(imageFile);
			imageBodyPart.setDataHandler(new DataHandler(fds));
			imageBodyPart.setFileName(MimeUtility.encodeWord(imageFile.getName()));
			multipart.addBodyPart(imageBodyPart);
			
			msg.setContent(multipart);
		
	}

	/**
	 * 发送内嵌图片和附件邮件
	 */
	private static void sendImageAndAttachment(Message msg) throws MessagingException, UnsupportedEncodingException{
			// 正文
			MimeBodyPart text = new MimeBodyPart();
			text.setContent("我的头像：<img src='cid:headImg'>", "text/html;charset=UTF-8");

			// 正文图片
			MimeBodyPart image = new MimeBodyPart();
			image.setDataHandler(new DataHandler(new FileDataSource("1_jianggujin.jpg")));
			image.setContentID("headImg");

			// 附件
			MimeBodyPart attach = new MimeBodyPart();
			DataHandler dh = new DataHandler(new FileDataSource("1_jianggujin.jpg"));
			attach.setDataHandler(dh);
			attach.setFileName(MimeUtility.encodeWord(dh.getName()));

			// 描述关系:正文和图片
			MimeMultipart mp1 = new MimeMultipart();
			mp1.addBodyPart(text);
			mp1.addBodyPart(image);
			mp1.setSubType("related");

			// 正文
			MimeBodyPart content = new MimeBodyPart();
			content.setContent(mp1);

			MimeMultipart multipart = new MimeMultipart("mixed");
			multipart.addBodyPart(content);
			multipart.addBodyPart(attach);

			msg.setContent(multipart);
			
			
			
			msg.setContent(multipart);
	}

}
